package com.bluesky.plum.uimodels.standard.components.validator;

import com.bluesky.plum.uimodels.standard.FieldUIComponent;
import com.bluesky.plum.uimodels.standard.StandardUIComponentType;
import com.bluesky.plum.uimodels.standard.UIComponent;
import com.bluesky.plum.uimodels.standard.components.Label;
import com.bluesky.plum.validate.validator.Validator;

/**
 * it is a base class, will actually do nothing.
 * 
 * @author jack
 * 
 */
public class ComponentValidator extends UIComponent {
	protected Validator validator;
	/**
	 * used to show message
	 */
	protected Label label;

	protected String description;

	/**
	 * the target component
	 */
	protected FieldUIComponent fieldUiComponent;

	public ComponentValidator() {
		description = "required";
	}

	@Override
	public void initialize() {
		super.initialize();

		label = (Label) uiComponentBuilder
				.createNonFieldComponent(StandardUIComponentType.Label);

		// create a real validator instance in the sub class.
	}

	public void validate() {
		validator.setTarget(fieldUiComponent.getDomainFieldValue());
		validator.validate();

	}

	public FieldUIComponent getFieldUiComponent() {
		return fieldUiComponent;
	}

	public void setFieldUiComponent(FieldUIComponent fieldUiComponent) {
		this.fieldUiComponent = fieldUiComponent;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
		label.setCaption(description);
	}

	public boolean getResult() {
		return validator.getResult();
	}

}
